package com.alleniverrui.mailsend.service;



import com.alleniverrui.mailsend.entity.CheckNumber;
import com.alleniverrui.mailsend.entity.RedisConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
//@PropertySource(value="classpath:application.properties",encoding = "utf-8")
public class MailService {

    @Value("${fromEmail}")
    private String fromEmail;

    @Resource
    public JavaMailSender javaMailSender;

    @Autowired
    public RedisService redisService;

    /**
     * 发送验证码给用户
     *
     * @param email 用户邮箱
     * @return 用户验证码
     */
    public CheckNumber sendCheckNumber(String email) throws Exception{
        CheckNumber checkNumber = new CheckNumber();
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);

        message.setFrom(fromEmail);
        message.setTo(email);
        message.setSubject("集美小窝");
        message.setText("<html >\n" +
                "<meta charset=\"utf-8\">\n" +
                "<body><div id=\"contentDiv\" onmouseover=\"getTop().stopPropagation(event);\" onclick=\"getTop().preSwapLink(event, 'html', 'ZC0004_vDfNJayMtMUuKGIAzzsWvc8');\" style=\"position:relative;font-size:14px;height:auto;padding:15px 15px 10px 15px;z-index:1;zoom:1;line-height:1.7;\" class=\"body\">\n" +
                "  <div id=\"qm_con_body\">\n" +
                "    <div id=\"mailContentContainer\" class=\"qmbox qm_con_body_content qqmail_webmail_only\" style=\"opacity: 1;\">\n" +
                "      <style type=\"text/css\">\n" +
                "        .qmbox h1,.qmbox \t\t\th2,.qmbox \t\t\th3 {\t\t\t\tcolor: #e11e1e;\t\t\t}\t\t\t.qmbox p {\t\t\t\tpadding: 0;\t\t\t\tmargin: 0;\t\t\t\tcolor: #333;\t\t\t\tfont-size: 16px;\t\t\t}\t\t\t.qmbox hr {\t\t\t\tbackground-color: #d9d9d9;\t\t\t\tborder: none;\t\t\t\theight: 1px;\t\t\t}\t\t\t.qmbox .eo-link {\t\t\t\tcolor: #0576b9;\t\t\t\ttext-decoration: none;\t\t\t\tcursor: pointer;\t\t\t}\t\t\t.qmbox .eo-link:hover {\t\t\t\tcolor: #3498db;\t\t\t}\t\t\t.qmbox .eo-link:hover {\t\t\t\ttext-decoration: underline;\t\t\t}\t\t\t.qmbox .eo-p-link {\t\t\t\tdisplay: block;\t\t\t\tmargin-top: 20px;\t\t\t\tcolor: #009cff;\t\t\t\ttext-decoration: underline;\t\t\t}\t\t\t.qmbox .p-intro {\t\t\t\tpadding: 30px;\t\t\t}\t\t\t.qmbox .p-code {\t\t\t\tpadding: 0 30px 0 30px;\t\t\t}\t\t\t.qmbox .p-news {\t\t\t\tpadding: 0px 30px 30px 30px;\t\t\t}\n" +
                "      </style>\n" +
                "      <div style=\"max-width:800px;padding-bottom:10px;margin:20px auto 0 auto;\">\n" +
                "        <table cellpadding=\"0\" cellspacing=\"0\" style=\"background-color: #fff;border-collapse: collapse; border:1px solid #e5e5e5;box-shadow: 0 10px 15px rgba(0, 0, 0, 0.05);text-align: left;width: 100%;font-size: 14px;border-spacing: 0;\">\n" +
                "          <tbody>\n" +
                "          <tr style=\"background-color: #f8f8f8;\">\n" +
                "            <td>\n" +
                "              <img style=\"padding: 15px 0 15px 30px;width:50px\" src=\"https://s3.bmp.ovh/imgs/2023/02/03/62d56eb6b3034807.jpg\">                <span > 集美小窝. </span>\n" +
                "            </td>\n" +
                "          </tr>\n" +
                "          <tr>\n" +
                "            <td class=\"p-intro\">\n" +
                "              <h1 style=\"font-size: 26px; font-weight: bold;\">验证您的邮箱地址</h1>\n" +
                "              <p style=\"line-height:1.75em;\">感谢您使用  集美小窝. </p>\n" +
                "              <p style=\"line-height:1.75em;\">若非别人操作请忽视此信息</p>\n" +
                "              <p style=\"line-height:1.75em;\">以下是您的邮箱验证码，请将它输入到  集美小窝 的邮箱验证码输入框中:</p>\n" +
                "\n" +
                "            </td>\n" +
                "          </tr>\n" +
                "          <tr>\n" +
                "            <td class=\"p-code\">\n" +
                "              <p style=\"color: #253858;text-align:center;line-height:1.75em;background-color: #f2f2f2;min-width: 200px;margin: 0 auto;font-size: 28px;border-radius: 5px;border: 1px solid #d9d9d9;font-weight: bold;\">"+checkNumber.getNumber()+" </p>\n" +
                "            </td>\n" +
                "          </tr>\n" +
                "          <tr>\n" +
                "            <td class=\"p-intro\">\n" +
                "              <p style=\"line-height:1.75em;\">这一封邮件包括一些您的 集美小窝 账号信息，请不要回复或转发它，以免带来不必要的信息泄露风险。 </p>\n" +
                "            </td>\n" +
                "          </tr>\n" +
                "          <tr>\n" +
                "            <td class=\"p-intro\">\n" +
                "              <hr>\n" +
                "              <p style=\"text-align: center;line-height:1.75em;\"> 集美小窝</p>\n" +
                "            </td>\n" +
                "          </tr>\n" +
                "          </tbody>\n" +
                "        </table>\n" +
                "      </div>\n" +
                "      <style type=\"text/css\">\n" +
                "        .qmbox style, .qmbox script, .qmbox head, .qmbox link, .qmbox meta {display: none !important;}\n" +
                "      </style>\n" +
                "    </div>\n" +
                "  </div><!-- -->\n" +
                "  <style>\n" +
                "    #mailContentContainer .txt {height:auto;}\n" +
                "  </style>\n" +
                "</div></body>\n" +
                "</html>\n" ,true);
        log.info("发送的邮件信息{}", checkNumber.getNumber());
        //        验证码存入redis
        redisService.setCacheObject(RedisConstants.EMAIL_CODE+ email, checkNumber.getNumber() +"");
        redisService.expire(RedisConstants.EMAIL_CODE+ email,RedisConstants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
        javaMailSender.send(mimeMessage);
        return checkNumber;
    }
}
